package com.yoshino.interpreter;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import java.util.Scanner;
import java.util.Stack;

/**
 * @author yoshino
 **/
public class Client {
    public static void main(String[] args) throws IOException {
        String expStr = getExpStr();// a + b
        HashMap<String, Integer> var = expStr(expStr); // var { a = 10, b = 20}
        Calculator calculator = new Calculator(expStr);
        System.out.println("运算结果：" + expStr + "=" + calculator.run(var));
    }

    //获得表达式
    public static String getExpStr() throws IOException {
        System.out.print("请输入表达式：");
        return (new BufferedReader(new InputStreamReader(System.in))).readLine();
    }

    public static HashMap<String, Integer> expStr(String expstr) {
        HashMap<String, Integer> map = new HashMap<>();
        // [a, +, b]
        char[] charArray = expstr.toCharArray();
        Scanner scanner = new Scanner(System.in);
        //遍历字符数组
        for (int i = 0; i < charArray.length; i++) {
            switch (charArray[i]) {
                case '+':
                    break;
                case '-':
                    break;
                default:
                    //如果是一个Var就创建要给VarExpression对象，并push到stack
                    System.out.print("请输入" + charArray[i] + "的值:");
                    int param = scanner.nextInt();
                    map.put(String.valueOf(charArray[i]), param);
                    break;
            }
        }
        //当遍历完整个charArray数组后，stack就得到最后Expression
        return map;
    }
}
